package com.jctrip.hotel.boot.config;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

/**
 * @Author henry
 * @Date 2023/7/4 13:30
 * @Description
 */
@Component
@EnableAsync
public class AsyncExecutePoolConfig {

    private static final int CORE_POOL_SIZE = 4;
    private static final int MAC_POOL_SIZE = 9;
    private static final int QUEUE_CAPACITY = 1024;
    private static final int KEEP_ALIVE_SECOND = 0;

    @Bean
    public Executor taskAsyncPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_POOL_SIZE); //核心线程数
        executor.setMaxPoolSize(MAC_POOL_SIZE);  //最大线程数
        executor.setQueueCapacity(QUEUE_CAPACITY); //队列大小
        executor.setKeepAliveSeconds(KEEP_ALIVE_SECOND); //线程最大空闲时间
        executor.setThreadNamePrefix("async-Executor-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略（一共四种，此处省略）
        executor.initialize();
        return executor;
    }

    @Bean
    public Executor callBackTaskAsyncPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_POOL_SIZE); //核心线程数
        executor.setMaxPoolSize(MAC_POOL_SIZE);  //最大线程数
        executor.setQueueCapacity(QUEUE_CAPACITY); //队列大小
        executor.setKeepAliveSeconds(KEEP_ALIVE_SECOND); //线程最大空闲时间
        executor.setThreadNamePrefix("callBackTaskAsyncPool-Executor");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略（一共四种，此处省略）
        executor.initialize();
        return executor;
    }

    @Bean
    public Executor meituanOrderCallbackTaskAsyncPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(CORE_POOL_SIZE);
        // 最大线程数
        executor.setMaxPoolSize(MAC_POOL_SIZE);
        // 队列大小
        executor.setQueueCapacity(QUEUE_CAPACITY);
        // 线程最大空闲时间
        executor.setKeepAliveSeconds(KEEP_ALIVE_SECOND);
        executor.setThreadNamePrefix("meituanOrderCallbackTaskAsyncPool-Executor");
        // 拒绝策略（一共四种，此处省略）
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}
